package com.rc.evcharger.common.terminal;


import com.rc.evcharger.common.annotation.ByteLength;
import com.rc.evcharger.common.utils.DataUtil;

import java.lang.reflect.Field;
import java.math.BigDecimal;

/**
 * (CMD=C5) 充电桩动态信息查询指令（桩响应）
 */
public class ElecPilePowerResult {

    @ByteLength(length = 2, decimal = 1)
    private BigDecimal outPower;//充电桩总的实时输出功率

    @ByteLength(length = 2, decimal = 1)
    private BigDecimal outPowerA;//A 枪输出功率

    @ByteLength(length = 2, decimal = 1)
    private BigDecimal outPowerB;//B 枪输出功率

    @ByteLength(length = 2, decimal = 1)
    private BigDecimal power;//桩功率

    @ByteLength(length = 4)
    private String obligate; // 预留字段;

    @ByteLength(length = 1)
    private String gunNumA; // 枪号 1：A枪 2：B枪

    @ByteLength(length = 3)
    private Integer bmsCurrentA;//整车动力蓄电池系统额定容量/Ah

    @ByteLength(length = 3)
    private Integer bmsVoltageA;//整车动力蓄电池系统额定总电压/V

    @ByteLength(length = 2)
    private Integer remainingTimeA;//车估算剩余充电时间

    @ByteLength(length = 2)
    private Integer usedTimeA;//车已充电时间

    @ByteLength(length = 1)
    private String gunNumB; // 枪号 1：A枪 2：B枪

    @ByteLength(length = 3)
    private Integer bmsCurrentB;

    @ByteLength(length = 3)
    private Integer bmsVoltageB;

    @ByteLength(length = 2)
    private Integer remainingTimeB;

    @ByteLength(length = 2)
    private Integer usedTimeB;


    @ByteLength(length = 4)
    private String obligate2; // 预留字段;

    public String getObligate2() {
        return obligate2;
    }

    public void setObligate2(String obligate2) {
        this.obligate2 = obligate2;
    }

    public BigDecimal getOutPower() {
        return outPower;
    }

    public void setOutPower(BigDecimal outPower) {
        this.outPower = outPower;
    }

    public BigDecimal getOutPowerA() {
        return outPowerA;
    }

    public void setOutPowerA(BigDecimal outPowerA) {
        this.outPowerA = outPowerA;
    }

    public BigDecimal getOutPowerB() {
        return outPowerB;
    }

    public void setOutPowerB(BigDecimal outPowerB) {
        this.outPowerB = outPowerB;
    }

    public BigDecimal getPower() {
        return power;
    }

    public void setPower(BigDecimal power) {
        this.power = power;
    }

    public String getObligate() {
        return obligate;
    }

    public void setObligate(String obligate) {
        this.obligate = obligate;
    }

    public String getGunNumA() {
        return gunNumA;
    }

    public void setGunNumA(String gunNumA) {
        this.gunNumA = gunNumA;
    }

    public Integer getBmsCurrentA() {
        return bmsCurrentA;
    }

    public void setBmsCurrentA(Integer bmsCurrentA) {
        this.bmsCurrentA = bmsCurrentA;
    }

    public Integer getBmsVoltageA() {
        return bmsVoltageA;
    }

    public void setBmsVoltageA(Integer bmsVoltageA) {
        this.bmsVoltageA = bmsVoltageA;
    }

    public Integer getRemainingTimeA() {
        return remainingTimeA;
    }

    public void setRemainingTimeA(Integer remainingTimeA) {
        this.remainingTimeA = remainingTimeA;
    }


    public String getGunNumB() {
        return gunNumB;
    }

    public void setGunNumB(String gunNumB) {
        this.gunNumB = gunNumB;
    }

    public Integer getBmsCurrentB() {
        return bmsCurrentB;
    }

    public void setBmsCurrentB(Integer bmsCurrentB) {
        this.bmsCurrentB = bmsCurrentB;
    }

    public Integer getBmsVoltageB() {
        return bmsVoltageB;
    }

    public void setBmsVoltageB(Integer bmsVoltageB) {
        this.bmsVoltageB = bmsVoltageB;
    }

    public Integer getRemainingTimeB() {
        return remainingTimeB;
    }

    public void setRemainingTimeB(Integer remainingTimeB) {
        this.remainingTimeB = remainingTimeB;
    }

    public Integer getUsedTimeA() {
        return usedTimeA;
    }

    public void setUsedTimeA(Integer usedTimeA) {
        this.usedTimeA = usedTimeA;
    }

    public Integer getUsedTimeB() {
        return usedTimeB;
    }

    public void setUsedTimeB(Integer usedTimeB) {
        this.usedTimeB = usedTimeB;
    }

    public ElecPilePowerResult() {
        super();
    }

    /**
     * 遍历所有字段，按顺序和长度截取数据
     *
     * @param str    数据域
     * @param source 硬件产商
     */
    public ElecPilePowerResult(String str, String source) {
        try {
            Field[] fields = this.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                ByteLength bl = fields[i].getAnnotation(ByteLength.class);
                if (bl == null) {
                    continue;
                }
                int len = bl.length() * 2 + bl.length() - 1;
                String value = str.substring(0, len);
                if (str.length() > len)
                    str = str.substring(len + 1);
                setValue(fields[i], bl, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void setValue(Field field, ByteLength bl, String value) {
        try {
            if (field.getType().equals(BigDecimal.class)) {
                field.set(this, DataUtil.getBDValue(value, bl.decimal(), true));
            } else if (field.getType().equals(Integer.class)) {
                field.set(this, DataUtil.getBDValue(value, bl.decimal(), true).intValue());
            } else {
                field.set(this, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
